//
//  SEImageFilesManager.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/30.
//

import Foundation
import Kingfisher

class SEImageFilesManager {
    private static let imageCache = KingfisherManager.shared.cache
    
    @discardableResult
    class func cachePath(forKey key: String) -> String {
        return imageCache.diskStorage.directoryURL.absoluteString + "/" + key
    }
    
    class func storeImage(_ image: UIImage, key: String, completion: ((CacheStoreResult) -> Void)? = nil) {
        imageCache.removeImage(forKey: key)
        imageCache.store(image, forKey: key, completionHandler: completion)
    }
    
    class func removeImage(forKey key: String) {
        imageCache.removeImage(forKey: key)
    }
    
    class func image(forKey key: String) -> UIImage? {
        var image: UIImage?
        imageCache.retrieveImage(forKey: key) { (result) in
            switch result {
            case .success(let cache):
                image = cache.image
            case .failure:
                image = nil
            }
        }
        return image
    }
}
